/**
* Javscript Source code file for fleet_movements.php
*/

function show_all_fleets() {
	dijit.byId("fleet_movement_details").set("href","ajax/ajax_fleetmove_all.php");
	window.location.hash = "#all";
}

function show_fleet_by_id(id) {
	dijit.byId("fleet_movement_details").set("href","ajax/ajax_fleetmove_id.php?fleet_id="+id);
	window.location.hash = "#id="+id;
}

function show_fleet_by_planet(coordinates) {
	var coords = coordinates.split(":");
	if (coords.length != 3) return;
	dijit.byId("fleet_movement_details").set("href","ajax/ajax_fleetmove_all.php?galaxy="+coords[0]+"&system="+coords[1]+"&planet="+coords[2]);
	window.location.hash = "#id="+id;
}

function deleteFleetMovement(fleet_id,sub_fleet_id) {
	
	var xhrArgs = {
	        url: "ajax/ajax_fleet_movement.php?fleet_id="+fleet_id+"&sub_fleet_id="+sub_fleet_id+"&action=delete",
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        		return;
	        	}
	        	
	        	// remove content
	        	var html_id = "fleet_"+fleet_id+"_"+sub_fleet_id;
	        	try {
	        		document.getElementById(html_id).parentNode.removeChild(document.getElementById(html_id));
	        	} catch(e) { 
	        		alert("Error: "+e); 
	        	}
	        },
	        error: function(error) {
	        	// can happen if another dojo xhr request is triggered and the old one is aborted
	        }
	    }	
	dojo.xhrPost(xhrArgs);
}

function galaxytool_fm_init() {
	// select user defined id or all
	var choosen_fleet = window.location.hash;
	if (choosen_fleet.indexOf("#id=") > -1) {
		choosen_fleet = choosen_fleet.replace(/#id=/, "");
		show_fleet_by_id(choosen_fleet);
	} else if(choosen_fleet.indexOf("#planet=") > -1) {
		choosen_fleet = choosen_fleet.replace(/#planet=/, "");
		show_fleet_by_planet(choosen_fleet);
	} else {
		show_all_fleets();
	}
}
dojo.ready(galaxytool_fm_init);